package pl.edu.mimuw.irs.rcp.view.navigation.handler;

import java.io.File;

import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.handlers.HandlerUtil;

import pl.edu.mimuw.irs.rcp.controller.editor.workflow.handler.AbstractSelectionHandler;
import pl.edu.mimuw.irs.rcp.persistence.descriptor.HistoryFilesDescriptor;
import pl.edu.mimuw.irs.rcp.util.Logger;
import pl.edu.mimuw.irs.rcp.util.MessageCode;

public class AddHistoryHandler extends AbstractSelectionHandler {

	// *** Attributes

	// *** Methods

	@Override
	protected Object execute(ExecutionEvent event, Object[] selection)
			throws ExecutionException {
		Shell shell = HandlerUtil.getActiveShell(event);
		
		for (Object object : selection) {
			if (!(object instanceof HistoryFilesDescriptor)) continue;
			HistoryFilesDescriptor descriptor = (HistoryFilesDescriptor) object;
			FileDialog dialog = new FileDialog(shell);
			
			String path = dialog.open();
			try {
				descriptor.addFile(new File(path));
			} catch (Exception e) {
				Logger.handle(MessageCode.HISTORY_SAVE_DESCRIPTOR_PROBLEM, e, path);
			}
		}
		
		return null;
	}
}
